package com.mac.web.core.config;

import com.mac.bill.domain.BillTel;
import com.mac.bill.service.IBillTelService;
import com.mac.common.utils.DateUtils;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.*;
import java.util.stream.Collectors;

@Configuration
public class CallDataSimulation {

    private static final String PATTERN = "yyyy-MM-dd HH:mm:ss";

    @Autowired
    private IBillTelService billTelService;

    //@Scheduled(cron = "0 0/2 * * * ?")
    public void callToReceive() {
        System.out.println("定时任务开始...");
        List<BillTel> billTels = billTelService.selectBillTelList(new BillTel());

        //吸毒电话
        List<String> _non_normal_list = billTels.stream().filter(t -> {
            return t.getSendTel().startsWith("028") || t.getSendTel().startsWith("0571") ||
                    t.getReceiveTel().startsWith("028") || t.getReceiveTel().startsWith("0571");
        }).map(BillTel::getSendTel).collect(Collectors.toList());


        //正常用户
        List<String> normal_list =
                billTels.stream().filter(t -> {
                    return !t.getSendTel().startsWith("0571") && !t.getSendTel().startsWith("028");
                }).map(BillTel::getSendTel).collect(Collectors.toList());

        for (int i = 0; i < _non_normal_list.size(); i++) {
            saveData(_non_normal_list, _non_normal_list, new StringBuilder(normal_list.get(i)), i);
        }
        System.out.println("定时任务结束...");
    }

    //@Scheduled(cron = "0/10 * * * * ?")
    public void dataSimulation() {

        System.out.println("定时任务开始...");
        // 模拟028开头的 9位数的吸毒电话
        StringBuilder send_028_tel = new StringBuilder("028");
        List<String> receive_028_tel_List = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            send_028_tel.append(RandomUtils.nextInt());
            receive_028_tel_List.add(send_028_tel.toString());
            send_028_tel = new StringBuilder("028");
        }

        // 模拟0571开头的 12位数的吸毒电话
        StringBuilder send_0571_tel = new StringBuilder("0571");
        List<String> receive_0571_tel_List = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            send_0571_tel.append(RandomUtils.nextInt());
            receive_0571_tel_List.add(send_0571_tel.toString());
            send_0571_tel = new StringBuilder("0571");
        }

        // 模拟15x开头的 11位数的正常电话
        StringBuilder send_15x_tel;
        for (int i = 0; i < 10; i++) {
            send_15x_tel = new StringBuilder("15");
            saveData(receive_028_tel_List, receive_0571_tel_List, send_15x_tel, i);
        }

        // 模拟16x开头的 11位数的正常电话
        StringBuilder send_16x_tel;
        for (int i = 0; i < 10; i++) {
            send_16x_tel = new StringBuilder("16");
            saveData(receive_028_tel_List, receive_0571_tel_List, send_16x_tel, i);
        }

        // 模拟17x开头的 11位数的正常电话
        StringBuilder send_17x_tel;
        for (int i = 0; i < 10; i++) {
            send_17x_tel = new StringBuilder("17");
            saveData(receive_028_tel_List, receive_0571_tel_List, send_17x_tel, i);
        }

        // 模拟18x开头的 11位数的正常电话
        StringBuilder send_18x_tel;
        for (int i = 0; i < 10; i++) {
            send_18x_tel = new StringBuilder("18");
            saveData(receive_028_tel_List, receive_0571_tel_List, send_18x_tel, i);
        }

        // 模拟19x开头的 11位数的正常电话
        StringBuilder send_19x_tel;
        for (int i = 0; i < 10; i++) {
            send_19x_tel = new StringBuilder("19");
            saveData(receive_028_tel_List, receive_0571_tel_List, send_19x_tel, i);
        }

        System.out.println("定时任务结束...");
    }

    private void saveData(List<String> receive_028_tel_List, List<String> receive_0571_tel_List, StringBuilder send_tel, int i) {
        BillTel billTel = new BillTel();
        send_tel.append(RandomUtils.nextInt());
        String str = send_tel.toString();
        if (send_tel.length() > 11) {
            str = send_tel.substring(0, 11);
        }
        if (i / 4 == 0) {
            billTel.setSendTel(str);
            billTel.setReceiveTel(receive_028_tel_List.get(i));
        } else if (i / 4 == 1) {
            billTel.setSendTel(str);
            billTel.setReceiveTel(receive_0571_tel_List.get(i));
        } else if (i / 4 == 2) {
            billTel.setReceiveTel(str);
            billTel.setSendTel(receive_028_tel_List.get(i));
        } else if (i / 4 == 3) {
            billTel.setReceiveTel(str);
            billTel.setSendTel(receive_0571_tel_List.get(i));
        }

        billTel.setStartAt(DateUtils.dateTimeNow(PATTERN));
        String endAt = DateFormatUtils.format(new Date().getTime() +
                Long.parseLong(String.valueOf(RandomUtils.nextInt()).substring(0, 7)), PATTERN);
        billTel.setEndAt(endAt);
        try {
            if (billTel.getSendTel() == null || billTel.getReceiveTel() == null) return;
            billTelService.insertBillTel(billTel);
            Thread.sleep(500);
        } catch (InterruptedException e) {
            System.out.println("有空数据，跳过。。。");
        }
    }

}
